<?php
class Blogger_Users extends Blogger_Db_Table_Abstract
{
	protected $_name = 'users';
	protected $_primary = 'id';
	
	public function register($fname, $lname, $email, $password, $alias)
	{
		$fname = addslashes($fname);
		$lname = addslashes($lname);
		$email = addslashes($email);
		$alias = addslashes($alias);
		if(null != $this->getUserByEmail($email))
			throw new Blogger_Exception("Given email $email is already in the system");
		if(null != $this->getUserByAlias($alias))
			throw new Blogger_Exception("Given alias $alias is already in the system");
		$this->insert(array('firstName' => $fname,
							'lastName' => $lname,
							'email' => $email,
							'password' => Blogger_Auth::generatePasswordSignature($password),
							'alias' => $alias,
						));
		return $this->getAdapter()->lastInsertId();
	}
	
	public function getUserById($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow("id = $id");
		if(empty($row))
			return null;
		else
			return array('id' => $row->id,
							'fname' => stripslashes($row->firstName),
							'lname' => stripslashes($row->lastName),
							'email' => stripslashes($row->email),
							'password' => $row->password,
							'alias' => $row->alias,
						);
	}
	
	public function getUserByEmail($email)
	{
		$email = addslashes($email);
		$row = $this->fetchRow("email = '$email'");
		if(empty($row))
			return null;
		else
			return array('id' => $row->id,
							'fname' => stripslashes($row->firstName),
							'lname' => stripslashes($row->lastName),
							'email' => stripslashes($row->email),
							'password' => $row->password,
							'alias' => $row->alias,
						);
	}
	
	public function getUserByAlias($alias)
	{
		$alias = addslashes($alias);
		$row = $this->fetchRow("alias = '$alias'");
		if(empty($row))
			return null;
		else
			return array('id' => $row->id,
							'fname' => stripslashes($row->firstName),
							'lname' => stripslashes($row->lastName),
							'email' => stripslashes($row->email),
							'password' => $row->password,
							'alias' => $row->alias,
						);
	}
	
}